package com.haitaoit.chengquan.utils;

import android.content.Context;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * 网络请求
 * <p>
 * HttpUtilsSingle
 * <p>
 * 刘霄鹏
 * <p>
 * 2015年3月18日 上午10:00:20
 *
 * @version 1.0.0
 */

public class HttpUtilsSingle {
    private static Context context;

    private HttpUtilsSingle() {
    }

    private static HttpUtilsSingle httputils = null;

    public static HttpUtilsSingle getInstentce() {
        if (httputils != null) {
            httputils = new HttpUtilsSingle();
        }
        return httputils;
    }

    public static final com.lidroid.xutils.HttpUtils http = new com.lidroid.xutils.HttpUtils(5000);

    static {
        http.configResponseTextCharset("UTF-8");
        http.configCurrentHttpCacheExpiry(0);// 设置超时时间
    }

    private static loadingDialog dialog = null;

    /**
     * get异步请求，无RequestParams params封装参数 Get(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 –
     * 可选) doGet(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context  上下文对象
     * @param isdialog 判断是否要启动加载进度dialog
     * @param url      请求地址
     * @param callBack 请求监听 void
     * @since 1.0.0
     */
    public static void doGet(Context context, boolean isdialog,
                             final String url, final RequestCallBack<String> callBack) {
        HttpUtilsSingle.context = context;
        if (HttpUtilsSingle.context != null) {
            dialog = loadingDialog.createDialog(HttpUtilsSingle.context);
            if (isdialog) {
                dialog.show();
            }
        }
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                Loger.e("GET--url:" + url);
                callBack.onStart();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                Loger.e("GET--请求过程:" + "total:" + total + "--current:"
                        + current + "--isUploading:" + isUploading);
                callBack.onLoading(total, current, isUploading);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                dialog.dismiss();
                Loger.e("GET--请求失败:" + msg);
                error.printStackTrace();
                callBack.onFailure(error, msg);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                dialog.dismiss();
                Loger.e("GET--请求成功:" + responseInfo.result.toString());
                callBack.onSuccess(responseInfo);
            }

            @Override
            public void onCancelled() {
                dialog.dismiss();
                Loger.e("POST--请求已取消");
                callBack.onCancelled();
            }
        });
    }

    /**
     * get异步请求，有RequestParams params封装参数 Get(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 –
     * 可选) doGet(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context  上下文对象
     * @param isdialog 判断是否启动加载dialog
     * @param url      请求地址
     * @param params   拼接的参数
     * @param callBack 请求监听 void
     * @since 1.0.0
     */
    public static void doGet(Context context, boolean isdialog,
                             final String url, final RequestParams params,
                             final RequestCallBack<String> callBack) {
        HttpUtilsSingle.context = context;
        if (HttpUtilsSingle.context != null) {
            dialog = loadingDialog.createDialog(HttpUtilsSingle.context);
            if (isdialog) {
                dialog.show();
            }
        }
        http.send(HttpRequest.HttpMethod.GET, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                Loger.e("GET--url:" + url);
                String str = params.getQueryStringParams().toString().replace(", ", "&");
                Loger.e("GET--params:" + str);
                callBack.onStart();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                Loger.e("GET--请求过程:" + "total:" + total + "--current:"
                        + current + "--isUploading:" + isUploading);
                callBack.onLoading(total, current, isUploading);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                dialog.dismiss();
                Loger.e("GET--请求失败:" + msg);
                error.printStackTrace();
                callBack.onFailure(error, msg);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                dialog.dismiss();
                Loger.e("GET--请求成功:" + responseInfo.result.toString());
                callBack.onSuccess(responseInfo);
            }

            @Override
            public void onCancelled() {
                dialog.dismiss();
                Loger.e("POST--请求已取消");
                callBack.onCancelled();
            }
        });
    }

    /**
     * post异步请求，无RequestParams params封装参数 Post(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 –
     * 可选) doPost(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context  上下文对象
     * @param isdialog 判断是不是要启动加载进度dialog
     * @param s
     *@param url      请求地址
     * @param callBack 请求监听 void   @since 1.0.0
     */
    public static void doPost(Context context, boolean isdialog, String s, final String url, final RequestCallBack<String> callBack) {
        HttpUtilsSingle.context = context;
        if (HttpUtilsSingle.context != null) {
            dialog = loadingDialog.createDialog(HttpUtilsSingle.context);
            if (isdialog) {
                dialog.show();
            }
        }

        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                Loger.e("POST--url:" + url);
                callBack.onStart();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                Loger.e("POST--请求过程:" + "total:" + total + "--current:"
                        + current + "--isUploading:" + isUploading);
                callBack.onLoading(total, current, isUploading);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                dialog.dismiss();
                Loger.e("POST--请求失败:" + msg);
                error.printStackTrace();
                callBack.onFailure(error, msg);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                dialog.dismiss();
                Loger.e("POST--请求成功:" + responseInfo.result.toString());
                callBack.onSuccess(responseInfo);
            }

            @Override
            public void onCancelled() {
                dialog.dismiss();
                Loger.e("POST--请求已取消");
                callBack.onCancelled();
            }
        });
    }

    /**
     * post异步请求，有RequestParams params封装参数 Post(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 –
     * 可选) doPost(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context  上下文对象
     * @param isdialog 判断是否要启动加载进度dialog
     * @param url      请求地址
     * @param json     请求 json参数
     * @param callBack 请求监听 void
     * @since 1.0.0
     */
    public static void doPost(Context context, boolean isdialog,
                              final String url, final JSONObject json,
                              final RequestCallBack<String> callBack) {
        HttpUtilsSingle.context = context;
        if (HttpUtilsSingle.context != null) {
            dialog = loadingDialog.createDialog(HttpUtilsSingle.context);
            if (isdialog) {
                dialog.show();
            }
        }
        RequestParams params = new RequestParams();
        try {
            params.setBodyEntity(new StringEntity(json.toString(), "UTF-8"));
            params.setHeader("Content-Type", "application/json");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                Loger.e("POST--url:" + url);
                Loger.e("POST--json:" + json.toString());
                callBack.onStart();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                Loger.e("POST--请求过程:" + "total:" + total + "--current:"
                        + current + "--isUploading:" + isUploading);
                callBack.onLoading(total, current, isUploading);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                dialog.dismiss();
                Loger.e("POST--请求失败:" + msg);
                callBack.onFailure(error, msg);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                dialog.dismiss();
                Loger.e("POST--请求成功:" + responseInfo.result.toString());
                callBack.onSuccess(responseInfo);
            }

            @Override
            public void onCancelled() {
                dialog.dismiss();
                Loger.e("POST--请求已取消");
                callBack.onCancelled();
            }
        });
    }

}
